package cn.zl.mapper;

import cn.yunbamboos.mybatis.annotation.CustomQuery;
import cn.yunbamboos.mybatis.annotation.CustomUpdate;
import cn.yunbamboos.mybatis.annotation.GenerateXML;
import cn.yunbamboos.mybatis.conditions.Conditions;
import cn.yunbamboos.mybatis.mapper.GenericMapper;
import cn.zl.model.User;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@GenerateXML
@Repository
public interface UserMapper extends GenericMapper<User> {

    /**
     * 登录调用
     *
     * @param loginName 登录名
     * @return {@code Optional<User>}
     */
    default Optional<User> queryByLoginName(String loginName){
        return this.queryForObject(Conditions.create("login_name", loginName));
    }

    /**
     * 判断login_name是否存在
     *
     * @param loginName 登录名
     * @return login_name 是否存在
     */
    default boolean existLoginName(@Param("login_name") String loginName){
        return this.queryForObject(Conditions.create("login_name", loginName)).isPresent();
    }

    @CustomUpdate(sql = "update `zl_user` set `password` = #{password} where `user_id` = #{user_id}")
    void updatePassword(@Param("user_id") long userId, @Param("password") String password);

    @CustomQuery(sql = "select count(1) from zl_user", resultType = Integer.class)
    int total();
}
